package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺状态的工作
 */
@RestController(value = "adminShopController")
@RequestMapping("/admin/shop")
@Slf4j
public class ShopController {
    //店铺状态key
    private static final String key="SHOP_STATUS";
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 设置店铺状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    public Result<String> setStatus(@PathVariable(value = "status")Integer status){
        log.info("设置店铺状态:"+ (StatusConstant.ENABLE.equals(status)?"营业中":"打烊中"));
        //设置缓存数据
        redisTemplate.opsForValue().set(key,status);
        return Result.success("设置店铺状态成功");
    }

    /**
     * 获取店铺状态
     * @return
     */
    @GetMapping("/status")
    public Result<Integer> getStatus(){
        Object object = redisTemplate.opsForValue().get(key);
        Integer status= (Integer) object;
        return Result.success(status);
    }
}
